package com.newlink.building.common_base.utils

import android.os.Build
import androidx.annotation.RequiresApi
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.util.*


/**
 * @Author: Jake
 * @Date: 2023-05-12
 * @Description:
 */
object TimezoneConvert {

    const val TIME_ZONE_ID_EN: String = "GMT"
    const val TIME_ZONE_ID_CN: String = "Asia/Shanghai"

    fun convertTimeFromGMTToLocal(
        time: String,
        oldTimeZoneId: String = TIME_ZONE_ID_EN,
        newTimeZoneID: String = TimeZone.getDefault().id,
        ignoreSecond: Boolean = false
    ): String {

        if (time.isNullOrEmpty()) {
            return ""
        }
        return try {
            val pattern = if (ignoreSecond) "yyyy-MM-dd HH:mm" else "yyyy-MM-dd HH:mm:ss"
            val commonTime = stringToDate(time, pattern, oldTimeZoneId)
            val sdf: SimpleDateFormat = SimpleDateFormat(pattern).apply {
                timeZone = TimeZone.getTimeZone(newTimeZoneID)
            }
            sdf.format(commonTime)
        } catch (e: Exception) {//格式化失败,返回原始格式
            time
        }
    }

    fun formatTimestamp(timestamp1: Long, ignoreSecond: Boolean = false): String {
        val timestamp = timestamp1 * 1000
        val currentTime = System.currentTimeMillis()
        val oneDayInMillis = 24 * 60 * 60 * 1000

        return if (currentTime - timestamp > oneDayInMillis) {
            // 大于1天，截取日期
            val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
            dateFormat.format(Date(timestamp))
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (isToday(timestamp)) {
                    val pattern = if (ignoreSecond) "HH:mm" else "HH:mm:ss"
                    val timeFormat = SimpleDateFormat(pattern, Locale.getDefault())

                    timeFormat.format(Date(timestamp))
                } else {
                    val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
                    dateFormat.format(Date(timestamp))
                }
            } else {
                // 小于1天，截取时间
                val pattern = if (ignoreSecond) "HH:mm" else "HH:mm:ss"
                val timeFormat = SimpleDateFormat(pattern, Locale.getDefault())
                timeFormat.format(Date(timestamp))
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun isToday(timestamp: Long): Boolean {
        val date: LocalDate =
            Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate()
        val today: LocalDate = LocalDate.now()
        return date.equals(today)
    }


    @Throws(ParseException::class)
    fun stringToDate(
        strTime: String?,
        formatType: String?,
        oldTimeZoneId: String = TIME_ZONE_ID_EN
    ): Date? {
        val formatter = SimpleDateFormat(formatType)
        formatter.timeZone = TimeZone.getTimeZone(oldTimeZoneId)
        var date: Date? = null
        date = formatter.parse(strTime)
        return date
    }

    @Throws(ParseException::class)
    fun longToDate(currentTime: Long, formatType: String): Date? {
        val dateOld = Date(currentTime) // 根据long类型的毫秒数生命一个date类型的时间
        val sDateTime: String = dateToString(dateOld, formatType) // 把date类型的时间转换为string
        return stringToDate(sDateTime, formatType)
    }

    @Throws(ParseException::class)
    fun stringToLong(strTime: String, formatType: String): Long {
        val date = stringToDate(strTime, formatType) // String类型转成date类型
        return date?.let { dateToLong(it) } ?: 0
    }

    fun dateToLong(date: Date): Long {
        return date.time
    }

    fun dateToString(date: Date, formatType: String): String {
        return SimpleDateFormat(formatType).format(date)
    }
}